<?php
// +----------------------------------------------------------------------
// | 后台首页
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\model\User as UserModel;
use app\common\model\Admin as AdminModel;
use think\facade\Session;

class Index extends Base {

    private $UserModel;
    private $AdminModel;

    public function _initialize() {
        parent::_initialize();
        $this->UserModel = new UserModel;
    }

    /**
     * 首页
     * @access public
     */
    public function index(){
        $this->assign('app_nav',siteplus_menu());
        return $this->fetch(); 
    }

    /**
     * 菜单
     * @access public
     * @return json
     */
    public function menu(){
        $id  = input('?get.id')?input('get.id/d'):0;
        $nav = siteplus_menu(); 
        return json($nav[$id]['menu']);
    }

    /**
     * 欢迎
     * @access public
     */
    public function welcome(){
        $pathMaps = [
            ['name'=>'<i class="iconfont icon-home"></i> '.lang('lang_ui_admin_index_title'),'url'=>'javascript:;']
        ];
        $this->assign('pathMaps',json_encode($pathMaps));
        return $this->fetch(); 
    }

    /**
     * 登录
     * @access public
     */
    public function login(){
//        var_dump(password_hash(md5('123456'),PASSWORD_DEFAULT));die;
        if(request()->isAjax()){
            $data = [
                'username' => input('post.username/s'),
                'password' => input('post.password/s')
            ];
            $validate = $this->validate($data,'User.login');
            if(true !== $validate){
                return json(['code'=>1,'message'=>$validate]);
            }
            $result  = $this->UserModel->login($data);
            if(!$result){
                return json(['code'=>1,'message'=>lang('lang_login_password_error')]);
            }else{
                $this->setLoginSession($result); 
                return json(['code'=>0,'message'=>lang('lang_login_password_success'),'data'=>['url'=>url('admin/index/index')]]);
            }
        }else{
            return $this->fetch(); 
        }
    }

    /**
     * 退出
     * @access public
     */
    public function logout(){
        $this->setlogoutSession(); 
        $this->redirect(url('admin/index/login'),302);
    } 
}